function getJsonCountry(data){
	var list=data.list
	var table="<div class='table-responsive'>" +
					"<table class='table table-th-block table-hover'>" +
						"<thead>" +
							"<tr>" +
								"<th>Image</th>" +
								"<th>Name</th>" +
								"<th>Action</th>" +
							"</tr>" +
						"</thead>" +
						"<tbody>";
		
			if(list.length>0){
				for(var i=0;i<list.length;i++){
					table+="<tr>" +
								"<td><img src='../upload/country/"+list[i].country_image+"' alt='image' class='avatar img-circle'/></td>";
					table+=		"<td>"+list[i].country_name+"</td>";
					table+=		"<td>" +
									"<a data-original-title='Delete' title='Delete' data-toggle='tooltip' href='#Delete' onclick=deleteCountry('delete-country-process.sp?page="+data.current_page+ "&id="+list[i].country_id+"')><i class='fa fa-times-circle icon-rounded icon-xs icon-warning'></i></a>&nbsp;" +
									"<a data-original-title='Update' title='Update' data-toggle='tooltip' href='update-country.sp?page="+data.current_page+ "&id="+list[i].country_id+"'><i class='fa fa-pencil icon-rounded icon-xs icon-info'></i></a>" +
								"</td>" +
							"</tr>";
				}
			}else{
				table +=  "<tr><td style='text-align:center' colspan=3 class='alert alert-danger alert-bold-border square fade in alert-dismissable'><h2>Search not found!</h2></td></tr>";
			}
	
		table+=			"</tbody>" +
					"</table>" +
				"</div>";
		
	//------------------------------------------------ Pagination -----------------------------------//
		var command="list-country-process.sp";//only change this line
		var pagination = "<div class='row'>" +
							"<div class='col-sm-3'>" +
								"<div class='dataTables_info' id='datatable-example_info'>" +
									"Page "+ data.current_page +" of "+ data.total_page +" entries" +
								"</div>" +
							"</div>" +
							"<div class='col-sm-9'>" +
								"<div class='dataTables_paginate paging_bootstrap'>" +
									"<ul class='pagination separated-square'>";	
		//first
		if(data.current_page > 1)
			pagination += "<li><a href='#page=1' onclick=goTo('"+command+"?page="+1 +"')>First</a></li>";
		//previous
		if(data.has_previous_page)
			pagination += "<li><a href='#pre="+data.previous_page +"' onclick=goTo('"+command+"?page="+data.previous_page +"') >Previous</a></li>";
		//behind current
		for(var i=data.current_page-4;i<data.current_page;i++){
			if(i>0)
				pagination += "<li><a href='#page="+i+"' onclick=goTo('"+command+"?page="+i +"')>"+i+"</a></li>";
		}
		//current
		pagination += "<li class='active'><a  href='#page="+data.current_page+"' onclick=goTo('"+command+"?page="+data.current_page +"')>"+data.current_page+"</a></li>";
		//in front of current
		for(var i=data.current_page+1; i<data.total_page+1; i++){
			pagination += "<li><a href='#page="+i+"'  onclick=goTo('"+command+"?page="+i +"')>"+i+"</a></li>";
			
			if(data.current_page==1){
				if(i>data.current_page+7 ){
					break;
				}
			}else{
				if(i>data.current_page+3 ){
					break;
				}	
			}
		}
		//next
		if(data.has_next_page)
			pagination += "<li><a href='#next' onclick=goTo('"+command+"?page="+data.next_page +"')>Next</a></li>";
		//last
		if(data.total_page!=data.current_page)
			pagination += "<li><a href='#page="+data.total_page+"' onclick=goTo('"+command+"?page="+data.total_page +"')>Last</a></li>";
		
		
		pagination+=				"</ul>" +
								"</div>" +
							"</div>" +
						"</div>";
	
	return (table+pagination);
}

function goTo(url){
	$.get(url+"&search="+$("#search").val()+"&limit="+$("#limit").val()
		,function(data){
		$("#country").html(getJsonCountry(data));			
	});
}

function deleteCountry(url){
	if (confirm("Are you sure you want to delete the country?")) {
		$.get(url+"&search="+$("#search").val()+"&limit="+$("#limit").val()
				,function(data){
				$("#country").html(getJsonCountry(data));			
			});
	} 
}


$(document).ready(function(){
	$("#limit").change(function(){
		$.post("list-country-process.sp" , 
				{ 
					
					limit : $("#limit").val(),
					search : $("#search").val()
					
				}, 
					function(data){
					$("#country").html(getJsonCountry(data));
		});
	});
	
	$("#search").keyup(function(){
		$.post("list-country-process.sp" , 
		{ 
			limit : $("#limit").val(),
			search : $("#search").val()
		}, 
			function(data){
			$("#country").html(getJsonCountry(data));
		});
	});

});

